﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using AmazonGlacierGUI.Core;
using AmazonGlacierGUI.Core.Helpers.AmazonGlacier;
using AmazonGlacierGUI.Core.Infrastructure;
using AmazonGlacierGUI.Core.Model;
using AmazonGlacierGUI.UI.Infrastructure;
using AmazonGlacierGUI.UI.Infrastructure.MVVMFramework;
using GalaSoft.MvvmLight.Command;
using Raven.Database.Tasks;
using System.Threading.Tasks;
using Task = System.Threading.Tasks.Task;

namespace AmazonGlacierGUI.UI.ViewModels
{
    //20-Sep-2012 Priyan R
    public class DownloadArchiveByArchiveIdViewModel : AppViewModelBase
    {        
        public DownloadArchiveByArchiveIdViewModel()
        {         
            
            
            
            
            WireCommands();
            AddValidation(() => SaveToPath,
                          () =>
                          {
                              if (!Directory.Exists(SaveToPath))
                              {
                                  return new ValidationResultData("Path not exist");
                              }
                              return ValidationResultData.Success;
                          });
            AddValidation(()=>ArchiveDetails,()=>
            {
                if (!ArchiveDetails.Any())
                {
                    return new ValidationResultData("No archive details entered");
                }
                return ValidationResultData.Success;
            });

            ArchiveDetails=new ObservableCollection<ArchiveDetailsVM>();
        }
        
        public override void OnInit()
        {
    
        }
        private void OnDownloadCommand()
        {            
            if(!ValidateAll()) return;            
            foreach (var archiveDetail in ArchiveDetails)
            {
                if (!archiveDetail.ValidateAll()) return;
            }
            foreach (var archiveDetail in ArchiveDetails)
            {
                var vm = new JobViewModel();
                vm.JobType=JobTypeEnum.Download;
                vm.DownloadArchiveId = archiveDetail.ArchiveId;
                vm.DownloadArchiveToPath = SaveToPath;
                vm.DownlodArchiveFileName = archiveDetail.SaveAsFileName;
                vm.VaultName = VaultName;
                App.JobsVM.AddJob(vm);
            }
            Close();
        }
        private void OnBrowseVaultCommand()
        {
            if (!ValidateProperty(() => Region)) return;
            var vm = new VaultBrowserViewModel(Region.Value);
            UIHelper.ShowVaultBrowser(vm);
            if (vm.SelectedVault != null)
            {
                VaultName = vm.SelectedVault.VaultName;
                Region = vm.Region;
            }

        }
        private void OnBrowseFolderCommand()
        {
            UIHelper.ShowSelectFolderDialog(
                (path) =>
                {
                    SaveToPath = path;
                }, () => { });
        }
        private void WireCommands()
        {
            DownloadCommand = new RelayCommand(OnDownloadCommand);
            BrowseVaultCommand = new RelayCommand(OnBrowseVaultCommand);
            BrowseFolderCommand = new RelayCommand(OnBrowseFolderCommand);
        }

     
        private ObservableCollection<ArchiveDetailsVM> _archiveDetails;
        public ObservableCollection<ArchiveDetailsVM> ArchiveDetails
        {
            get { return _archiveDetails; }
            set
            {
                if (_archiveDetails != value)
                {
                    _archiveDetails = value;                    
                    RaisePropertyChanged(() => ArchiveDetails);
                }
            }
        }

        private string _saveToPath;
        public string SaveToPath
        {
            get { return _saveToPath; }
            set
            {
                if (_saveToPath != value)
                {
                    _saveToPath = value;                    
                    RaisePropertyChanged(() => SaveToPath);
                }
            }
        }
        private string _vaultName;
        [Required]
        public string VaultName
        {
            get { return _vaultName; }
            set
            {
                if (_vaultName != value)
                {
                    _vaultName = value;
                    RaisePropertyChanged(() => VaultName);                 
                }
            }
        }
        private AmazonRegionEnum? _region;
        [Required]
        public AmazonRegionEnum? Region
        {
            get { return _region; }
            set
            {
                if (_region != value)
                {
                    _region = value;
                    RaisePropertyChanged(() => Region);                   
                }
            }
        }
        private bool _isLoading;
        public bool IsLoading
        {
            get { return _isLoading; }
            set
            {
                if (_isLoading != value)
                {
                    _isLoading = value;
                    RaisePropertyChanged(() => IsLoading);
                }
            }
        }
        public RelayCommand DownloadCommand { get; set; }
        public RelayCommand BrowseFolderCommand { get; set; }        
        public RelayCommand BrowseVaultCommand { get; set; }
        public class ArchiveDetailsVM:AppViewModelBase
        {
            public ArchiveDetailsVM()
            {
                AddValidation(() => SaveAsFileName,
                              () =>
                              {
                                  if(string.IsNullOrWhiteSpace(SaveAsFileName)==false&&AppUtility.IsValidFileName(SaveAsFileName)==false)
                                  {
                                      return new ValidationResultData("Invalid file name");
                                  }
                                  return ValidationResultData.Success;
                              });
                EnableValidtion = true;
            }            
            private string _archiveId;
            [Required]
            public string ArchiveId
            {
                get { return _archiveId; }
                set
                {
                    if (_archiveId != value)
                    {
                        _archiveId = value;             
                        RaisePropertyChanged(() => ArchiveId);
                        if (string.IsNullOrWhiteSpace(SaveAsFileName))
                            SaveAsFileName = value;
                    }
                }
            }

          
            private string _saveAsFileName;
            [Required]
            public string SaveAsFileName
            {
                get { return _saveAsFileName; }
                set
                {
                    if (_saveAsFileName != value)
                    {
                        _saveAsFileName = value;                        
                        RaisePropertyChanged(() => SaveAsFileName);
                    }
                }
            }
            
           
        }
    }
}